#include "Clean.h"

static void FreeFilterDeviceObject(IN PDEVICE_OBJECT DeviceObject)
{
    if (!DeviceObject) return;

    PDEVICE_EXTENSION lpDeviceExtension = DeviceObject->DeviceExtension;
    IoDetachDevice(lpDeviceExtension->AttachedToDeviceObject);
    IoDeleteDevice(DeviceObject);
}

void FreeControlDeviceObject()
{
    UNICODE_STRING usWin32Name = { 0 };

    if (g_lpDeviceObject)
        IoDeleteDevice(g_lpDeviceObject);

    RtlInitUnicodeString(&usWin32Name, USTRING_SYMBOL_NAME);
    IoDeleteSymbolicLink(&usWin32Name);
}

VOID DriverUnload(IN PDRIVER_OBJECT lpDriverObject)
{
    PDEVICE_OBJECT lpDeivceObject = lpDriverObject->DeviceObject;
    PDEVICE_OBJECT lpNextDeviceObject = NULL;

    while (lpDeivceObject)
    {
        lpNextDeviceObject = lpDeivceObject->NextDevice;

        if (CONTROL == GET_DEVICETYPE_FROM_EXTENSION(lpDeivceObject))
        {
            FreeControlDeviceObject();
        }
        else if (FILTER == GET_DEVICETYPE_FROM_EXTENSION(lpDeivceObject))
        {
            FreeFilterDeviceObject(lpDeivceObject);
        }

        lpDeivceObject = lpNextDeviceObject;
    }
}

